<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #div {
        border: solid;
      }
    </style>
  </head>
  <body>
    <div id="box">hello div</div>
    <script>
      const box = document.getElementById("box");
      box.onclick = function () {
        // 创建 textarea
        const textarea = document.createElement("textarea");
        // 将box中的内容放入textarea
        textarea.value = box.innerHTML;
        // 用textarea替换掉box
        box.replaceWith(textarea);

        // textarea失去焦点事件
        textarea.onblur = function () {
          box.innerHTML = textarea.value;
          textarea.replaceWith(box);
        };
        // 监听键盘Enter
        textarea.onkeydown = function (e) {
          if (e.key === "Enter") {
            // 取消textarea焦点
            textarea.blur();
          }
        };
      };
    </script>
  </body>
</html>
